import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import Home from '../pages/Home'
import Login from '../pages/Login'
import Register from '../pages/Register'
import Search from '../pages/Search'
import Detail from '../pages/Detail'
import AddCartSuccess from '../pages/AddCartSuccess'
import ShopCart from '../pages/ShopCart/index'
import Trade from '../pages/Trade/index'
import Pay from '../pages/Pay/index'
import PaySuccess from '../pages/PaySuccess/index'
import Center from '../pages/Center/index'
//引入二级路由组件
import myOrder from '../pages/Center/myOrder/index'
import groupOrder from '../pages/Center/groupOrder/index'

//引入仓库获得token
import store from '../store/vuex'

//push
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

//replace
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}

let router = new VueRouter({
  routes: [
    {
      path: '/home',
      component: Home,
      meta: { show: true }
    },
    {
      path: '/trade',
      component: Trade,
      meta: { isAuth:true },
      //路由独享守卫
      beforeEnter:(to,from,next)=>{
        if(from.path=='/shopcart'||from.path=='/login'){
           next()
        }else{
        next(false)
      }
    }
    },
    {
      path: '/pay',
      component: Pay,
      meta: { show: true,isAuth:true },
      beforeEnter:(to,from,next)=>{
        if(from.path=='/trade'){
           next()
        }else{
        next(false)
      }
    }
    },
    {
      path: '/paysuccess',
      component: PaySuccess,
      meta: { show: true,isAuth:true }
    },
    {
      path: '/center',
      component: Center,
      meta: { show: true,isAuth:true },
      children:[
       { path: 'myorder',
      component: myOrder,
      meta: {isAuth:true },    
    },
      { path: 'grouporder',
      component: groupOrder,
      meta: {isAuth:true },
    },
      { path: '/center',
      redirect:'myorder',},
      ]
    },
    {
      path: '/detail/:skuId',
      component: Detail,
      meta: { 
        show: true,
        keep:true
      }
    },
    //添加购物车页面
    {
      path: '/addcart',
      name: 'addcart',
      component: AddCartSuccess,
      meta: { show: true }
    },
    //购物车页面
    {
      path: '/shopcart',
      name: 'shopcart',
      component: ShopCart,
      meta: { show: true }
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { show: false }

    },
    {
      path: '/register',
      component: Register,
      meta: { show: false }
    },
    {
      path: '/search:keyword?',
      component: Search,
      name: 'search',
      meta: { show: false }
    },
    //从定向，在项目运行起来后，访问/立马跳转回首页
    {
      path: '/',
      redirect: '/home'
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { y: 0 }
  },
})
//路由全局守卫
router.beforeEach(async (to, from, next) => {
  let token = store.state.user.token
  let name = store.state.user.userinfo.name
  //用户已经登录，用户不能再去login
  if (token) {
    if (to.path === '/login') {
      next('/home')
    } else {
      //去的是其他路由不是login（用户已经登录）
      if (name) {
        next()
      } else {
        try {
          await store.dispatch('getUserinfo')
          next()
        } catch (error) {
          await store.dispatch('userlogout')
          next()
        }
      }

    }

  } else {
    //未登陆：不能去交易页/支付页/个人中心
    //未登录去上面这些页面跳转到登陆页面
    // let topath=to.path
    // if(topath.indexOf('/trade')!=-1||topath.indexOf('/pay')!=-1||
    // topath.indexOf('/paysuccess')!=-1||topath.indexOf('/center')!=-1)
    let isAuth=to.meta.isAuth
    let topath=to.path
    if(isAuth)
    {
      //把未登录去的路径存储在地址路径中
      next('/login?redirect='+topath)
    }else{
      next()
    }
  }

})


export default router